(function() {
    var app = angular.module("chinaums.study", []);
    app.controller('picUploadCtrl', function ($scope, $http, $q, $log) {
        $scope.logKeyword = "";

        $scope.submit = function(formData) {
            var header = {
                transformRequest: angular.identity,
                headers: {'Content-Type': undefined},
                withCredentials: false
            };
            var errorHander = function(error, status, headers, config) {
                $scope.errorMessage = error;
            };

            var params1 = new FormData();
            params1.append("pic", formData.pic1);
            var up1 = $http.post("pic/upload", params1, header).then(function (response) {
                $scope.img.pic1 = "pic/fetch/" + response.data.md5;
                return response.data;
            }, errorHander);

            var params2 = new FormData();
            params2.append("pic", formData.pic2);
            var up2 = $http.post("pic/upload", params2, header).then(function (response) {
                $scope.img.pic2 = "pic/fetch/" + response.data.md5;
                return response.data;
            }, errorHander);

            $q.all([up1, up2]).then(function (res) {
                $http.post("pic/pic-compare", {pic1:res[0].md5, pic2:res[1].md5}).then(function(response) {
                    $scope.result = response.data;
                    $scope.errorMessage = "";
                    $scope.queryLog();
                }, errorHander);
            });
        };

        $scope.queryLog = function() {
            $http.get("pic/query-log", {params:{keyword:$scope.logKeyword}}).then(function (response) {
                $scope.logs = response.data;
            }, function(error, status, headers, config) {
                $scope.errorMessage = error;
            });
        };

        $scope.queryLog();
    });
    app.directive("fileModel", function($parse) {
        return {
            scope: {
                fileModel:'=',
                errorMessage:'='
            },
            link: function($scope, elements, attrs) {
                var model = $parse(attrs.fileModel);
                elements.on('change', function() {
                    var file = elements[0].files[0];
                    if (file.size > 1 << 20) {
                        elements[0].value="";
                        $scope.errorMessage = "文件大小超过1M，请重新选择";
                    } else {
                        $scope.errorMessage = "";
                        $scope.fileModel = file;
                    }
                    $scope.$apply();
                });
            }
        };
    });
    app.filter("localDate", function () {
        return function(input) {
            if (input.year === undefined) {
                return input;
            }
            var date = new Date();
            date.setYear(input.year);
            date.setMonth(input.monthValue);
            date.setDate(input.dayOfMonth);
            return date;
        };
    });
    app.filter("localTime", function () {
        return function(input) {
            if (input.hour === undefined) {
                return input;
            }
            var date = new Date();
            date.setHours(input.hour);
            date.setMinutes(input.minute);
            date.setSeconds(input.second);
            return date;
        };
    });
})();